﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Business;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Crypto;
using System.Collections.Generic;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
   // [Authorize]
    [ApiController]
    public class RecipeConfigController : ControllerBase
    {
        private IRecipeConfigService _recipeConfigService;

        public RecipeConfigController(IRecipeConfigService recipeConfigService)
        {
            _recipeConfigService = recipeConfigService;
        }
        [HttpGet]
        public ApiResponseData GetAllRecipeConfigData()
        {
            List<RecipeConfig> model = _recipeConfigService.GetAllRecipeConfigs();
            return ResultHelper.Success(new ReturnData() { total = model.Count, dataList = model });
        }
        [HttpPost]
        public ApiResponseData GetRecipeConfigData(RecipeConfigInput input)
        {
            List<RecipeConfig> modelList = _recipeConfigService.GetAllRecipeConfigs();
            if (!input.ParameterId.IsNullOrEmpty())
            {
                modelList = modelList.Where(i => i.ParameterId.Contains(input.ParameterId)).ToList();
            }
            if (!input.RecipeTypeId.IsNullOrEmpty())
            {
                modelList = modelList.Where(i => i.RecipeTypeId == input.RecipeTypeId).ToList();
            }
            int count = modelList.Count;
            if (!modelList.IsNullOrEmpty())
            {
                int start = (input.page - 1) * input.size;
                int end = ((start + input.size) >= (modelList.Count)) ? (modelList.Count) : (start + input.size);
                modelList = modelList.GetRange(start, end - start);
            }
            List<RecipeConfigModel> models = _recipeConfigService.MendingRange(modelList);
            return ResultHelper.Success(new ReturnData() { total = count, dataList = models });
        }
        [HttpPost]
        public ApiResponseData AddRecipeConfig(RecipeConfig model)
        {
            //重复性判断
            if(_recipeConfigService.GetRecipeConfigById(model.ParameterId) != null)
            {
                return ResultHelper.Error(502, "ID cannot be repeated! ");
            }
            if (_recipeConfigService.addRecipeConfig(model))
            {
                return ResultHelper.Success("Add successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to add!");
            }
        }
        [HttpPost]
        public ApiResponseData UpdateRecipeConfig(RecipeConfig model)
        {
            if (_recipeConfigService.GetRecipeConfigById(model.ParameterId) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + model.ParameterId + "!");
            }
            if (_recipeConfigService.updateRecipeConfig(model))
            {
                return ResultHelper.Success("Update successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Update!");
            }
        }
        [HttpPost]
        public ApiResponseData DeleteRecipeConfig(DeleteStringId id)
        {
            if (_recipeConfigService.GetRecipeConfigById(id.id) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + id.id + "!");
            }
            if (_recipeConfigService.DeleteRecipeConfig(id.id))
            {
                return ResultHelper.Success("Delete successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Delete!");
            }
        }
        [HttpPost]
        public ApiResponseData BatchDelRecipeConfig(List<string> batchRemoveDatas)
        {
            if (batchRemoveDatas.Count > 0)
            {
                if (_recipeConfigService.BatchDelRecipeConfig(batchRemoveDatas))
                {
                    return ResultHelper.Success("Delete successfully!");
                }
                else
                {
                    return ResultHelper.Error(500, "Failed to Delete!");
                }
            }
            return ResultHelper.Error(502, "No data detected!");
        }
        [HttpGet]
        public ApiResponseData GetRecipeConfigByRecipeId(string recipeId) {
            List<RecipeConfig> model;
            if (!recipeId.IsNullOrEmpty())
            {
                model = _recipeConfigService.GetRecipeConfigsByRecipeId(recipeId);
            }
            else
            {
                model = new List<RecipeConfig>();
            };
            return ResultHelper.Success(new ReturnData() { total = model.Count, dataList = model });
        }


    }
}
